package com.fruit.controller.listener;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import com.fruit.constants.OrderConstant;
import com.fruit.pojo.order.DSHOrder;
import com.fruit.service.order.OrderConfirmManager;
import com.fruit.service.order.OrderHandel;
import com.fruit.service.order.impl.DelayService;
import com.fruit.service.order.impl.DelayService.OnDelayedListener;
import com.fruit.util.CustomUtil;

@Service  
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {  
      
  
    @Autowired
    private DelayService delayService;  
    
    @Autowired
    private OrderConfirmManager orderConfirmService;
    
    @Autowired
    private OrderHandel orderHandelService;
    
    
    protected Logger logger = LoggerFactory.getLogger(StartupListener.class);
    
    public final static  ExecutorService OrderThreadPool = Executors.newFixedThreadPool(5);  
      
    @Override  
    public void onApplicationEvent(ContextRefreshedEvent evt) {  
        if (evt.getApplicationContext().getParent() == null) {  
            return;  
        }  

        delayService.start(new OnDelayedListener() {
			
			@Override
			public void onDelayedArrived(final DSHOrder order) {
				
				OrderThreadPool.execute(new Runnable(){
					@Override
					public void run() {
						//处理订单
						logger.info("【订单处理开始】-【订单号】-"+order.getOrderId());
						if(order.getOrderId() != 0){
							if(OrderConstant.AUTO_CANCEL.equals(order.getType())){
								logger.info("【订单自动取消处理开始】-【订单号】-"+order.getOrderId());
								orderHandelService.autoCancelOrder(order, 3);
							}
						}
						logger.info("【订单取消结束】-【订单号】-"+order.getOrderId());
					}
				});
				
			}
		});
        
        OrderThreadPool.execute(new Runnable(){
			@Override
			public void run() {
				//订单初始化入队
				logger.info("【订单初始化入队开始】");
				try{
				DSHOrder order = new DSHOrder();
				order.setStart(0);
				order.setShowNumber(1000);			
				
				initDSHOrder(order);
				}catch(Exception e){
					e.printStackTrace();
				}
				
				logger.info("【订单初始化入队结束】");
			}
			
			public void initDSHOrder(DSHOrder order){
				List<DSHOrder> dshOrderList = orderConfirmService.getDshOrderList(order);
				if(dshOrderList !=null && dshOrderList.size() > 0){
					
					for(int i = 0; i< dshOrderList.size();i++){
						DSHOrder dshOrder = dshOrderList.get(i);
						
						delayService.add(dshOrder);
						if(i == (dshOrderList.size() -1)){
							order.setId(dshOrder.getId());
						}
					}
					
					if(dshOrderList.size() == order.getShowNumber()){
						initDSHOrder(order);
					}
				}

				
			}
			
		});
    }  
}  
